home *** CD-ROM | disk | FTP | other *** search
/ Delphi Developer's Kit 1996 / Delphi Developer's Kit 1996.iso / power / wfc007.000 / src / cnetwkst.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-22  |  15.5 KB  |  595 lines

  1. #include <wfc.h>
  2. #pragma hdrstop
  3.  
  4. /*
  5. ** Author: Samuel R. Blackburn
  6. ** CI$: 76300,326
  7. ** Internet: sammy@sed.csc.com
  8. **
  9. ** You can use it any way you like.
  10. */
  11.  
  12. #if defined( _DEBUG )
  13. #undef THIS_FILE
  14. static char BASED_CODE THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. /*
  18. ** CWorkstationUser stuff
  19. */
  20.  
  21. IMPLEMENT_SERIAL( CWorkstationUser, CObject, 1 )
  22.  
  23. CWorkstationUser::CWorkstationUser()
  24. {
  25.    m_Initialize();
  26. }
  27.  
  28. CWorkstationUser::~CWorkstationUser()
  29. {
  30.    m_Initialize();
  31. }
  32.  
  33. void CWorkstationUser::Copy( WKSTA_USER_INFO_1 *source )
  34. {
  35.    ASSERT( source != NULL );
  36.  
  37.    if ( source == NULL )
  38.    {
  39.       m_Initialize();
  40.       return;
  41.    }
  42.  
  43. #if ! defined( UNICODE )
  44.    ::UNICODE_to_ASCII( (LPCWSTR) source->wkui1_username,     source->wkui1_username     );
  45.    ::UNICODE_to_ASCII( (LPCWSTR) source->wkui1_logon_domain, source->wkui1_logon_domain );
  46.    ::UNICODE_to_ASCII( (LPCWSTR) source->wkui1_oth_domains,  source->wkui1_oth_domains  );
  47.    ::UNICODE_to_ASCII( (LPCWSTR) source->wkui1_logon_server, source->wkui1_logon_server );
  48. #endif
  49.  
  50.    UserName     = source->wkui1_username;
  51.    LogonDomain  = source->wkui1_logon_domain;
  52.    OtherDomains = source->wkui1_oth_domains;
  53.    LogonServer  = source->wkui1_logon_server;
  54.  
  55. #if ! defined( UNICODE )
  56.    ::ASCII_to_UNICODE( source->wkui1_username,     (LPWSTR) source->wkui1_username     );
  57.    ::ASCII_to_UNICODE( source->wkui1_logon_domain, (LPWSTR) source->wkui1_logon_domain );
  58.    ::ASCII_to_UNICODE( source->wkui1_oth_domains,  (LPWSTR) source->wkui1_oth_domains  );
  59.    ::ASCII_to_UNICODE( source->wkui1_logon_server, (LPWSTR) source->wkui1_logon_server );
  60. #endif
  61. }
  62.  
  63. void CWorkstationUser::Empty( void )
  64. {
  65.    m_Initialize();
  66. }
  67.  
  68. void CWorkstationUser::m_Initialize( void )
  69. {
  70.    UserName.Empty();
  71.    LogonDomain.Empty();
  72.    OtherDomains.Empty();
  73.    LogonServer.Empty();
  74. }
  75.  
  76. void CWorkstationUser::Serialize( CArchive& archive )
  77. {
  78.    CObject::Serialize( archive );
  79.  
  80.    if ( archive.IsStoring() )
  81.    {
  82.       archive << UserName;
  83.       archive << LogonDomain;
  84.       archive << OtherDomains;
  85.       archive << LogonServer;
  86.    }
  87.    else
  88.    {
  89.       archive >> UserName;
  90.       archive >> LogonDomain;
  91.       archive >> OtherDomains;
  92.       archive >> LogonServer;
  93.    }
  94. }
  95.  
  96. /*
  97. ** CWorkstationInformation stuff
  98. */
  99.  
  100. IMPLEMENT_SERIAL( CWorkstationInformation, CObject, 1 )
  101.  
  102. CWorkstationInformation::CWorkstationInformation()
  103. {
  104.    m_Initialize();
  105. }
  106.  
  107. CWorkstationInformation::~CWorkstationInformation()
  108. {
  109.    m_Initialize();
  110. }
  111.  
  112. void CWorkstationInformation::Copy( WKSTA_INFO_100 *source )
  113. {
  114.    ASSERT( source != NULL );
  115.  
  116.    if ( source == NULL )
  117.    {
  118.       m_Initialize();
  119.       return;
  120.    }
  121.  
  122. #if ! defined( UNICODE )
  123.    ::UNICODE_to_ASCII( (LPCWSTR) source->wki100_computername, source->wki100_computername );
  124.    ::UNICODE_to_ASCII( (LPCWSTR) source->wki100_langroup,     source->wki100_langroup     );
  125. #endif
  126.  
  127.    PlatformID            = source->wki100_platform_id;
  128.    ComputerName          = source->wki100_computername;
  129.    LANGroup              = source->wki100_langroup;
  130.    MajorVersion          = source->wki100_ver_major;
  131.    MinorVersion          = source->wki100_ver_minor;
  132.    LANRoot.Empty();;
  133.    NumberOfLoggedOnUsers = 0;
  134.  
  135. #if ! defined( UNICODE )
  136.    ::ASCII_to_UNICODE( source->wki100_computername, (LPWSTR) source->wki100_computername );
  137.    ::ASCII_to_UNICODE( source->wki100_langroup,     (LPWSTR) source->wki100_langroup     );
  138. #endif
  139. }
  140.  
  141. void CWorkstationInformation::Copy( WKSTA_INFO_101 *source )
  142. {
  143.    ASSERT( source != NULL );
  144.  
  145.    if ( source == NULL )
  146.    {
  147.       m_Initialize();
  148.       return;
  149.    }
  150.  
  151. #if ! defined( UNICODE )
  152.    ::UNICODE_to_ASCII( (LPCWSTR) source->wki101_computername, source->wki101_computername );
  153.    ::UNICODE_to_ASCII( (LPCWSTR) source->wki101_langroup,     source->wki101_langroup     );
  154.    ::UNICODE_to_ASCII( (LPCWSTR) source->wki101_lanroot,      source->wki101_lanroot      );
  155. #endif
  156.  
  157.    PlatformID            = source->wki101_platform_id;
  158.    ComputerName          = source->wki101_computername;
  159.    LANGroup              = source->wki101_langroup;
  160.    MajorVersion          = source->wki101_ver_major;
  161.    MinorVersion          = source->wki101_ver_minor;
  162.    LANRoot               = source->wki101_lanroot;
  163.    NumberOfLoggedOnUsers = 0;
  164.  
  165. #if ! defined( UNICODE )
  166.    ::ASCII_to_UNICODE( source->wki101_computername, (LPWSTR) source->wki101_computername );
  167.    ::ASCII_to_UNICODE( source->wki101_langroup,     (LPWSTR) source->wki101_langroup     );
  168.    ::ASCII_to_UNICODE( source->wki101_lanroot,      (LPWSTR) source->wki101_lanroot      );
  169. #endif
  170. }
  171.  
  172. void CWorkstationInformation::Copy( WKSTA_INFO_102 *source )
  173. {
  174.    ASSERT( source != NULL );
  175.  
  176.    if ( source == NULL )
  177.    {
  178.       m_Initialize();
  179.       return;
  180.    }
  181.  
  182. #if ! defined( UNICODE )
  183.    ::UNICODE_to_ASCII( (LPCWSTR) source->wki102_computername, source->wki102_computername );
  184.    ::UNICODE_to_ASCII( (LPCWSTR) source->wki102_langroup,     source->wki102_langroup     );
  185.    ::UNICODE_to_ASCII( (LPCWSTR) source->wki102_lanroot,      source->wki102_lanroot      );
  186. #endif
  187.  
  188.    PlatformID            = source->wki102_platform_id;
  189.    ComputerName          = source->wki102_computername;
  190.    LANGroup              = source->wki102_langroup;
  191.    MajorVersion          = source->wki102_ver_major;
  192.    MinorVersion          = source->wki102_ver_minor;
  193.    LANRoot               = source->wki102_lanroot;
  194.    NumberOfLoggedOnUsers = source->wki102_logged_on_users;
  195.  
  196. #if ! defined( UNICODE )
  197.    ::ASCII_to_UNICODE( source->wki102_computername, (LPWSTR) source->wki102_computername );
  198.    ::ASCII_to_UNICODE( source->wki102_langroup,     (LPWSTR) source->wki102_langroup     );
  199.    ::ASCII_to_UNICODE( source->wki102_lanroot,      (LPWSTR) source->wki102_lanroot      );
  200. #endif
  201. }
  202.  
  203. void CWorkstationInformation::Empty( void )
  204. {
  205.    m_Initialize();
  206. }
  207.  
  208. void CWorkstationInformation::m_Initialize( void )
  209. {
  210.    PlatformID            = 0;
  211.    ComputerName.Empty();
  212.    LANGroup.Empty();
  213.    MajorVersion          = 0;
  214.    MinorVersion          = 0;
  215.    LANRoot.Empty();
  216.    NumberOfLoggedOnUsers = 0;
  217. }
  218.  
  219. void CWorkstationInformation::Serialize( CArchive& archive )
  220. {
  221.    CObject::Serialize( archive );
  222.  
  223.    if ( archive.IsStoring() )
  224.    {
  225.       archive << PlatformID;
  226.       archive << ComputerName;
  227.       archive << LANGroup;
  228.       archive << MajorVersion;
  229.       archive << MinorVersion;
  230.       archive << LANRoot;
  231.       archive << NumberOfLoggedOnUsers;
  232.    }
  233.    else
  234.    {
  235.       archive >> PlatformID;
  236.       archive >> ComputerName;
  237.       archive >> LANGroup;
  238.       archive >> MajorVersion;
  239.       archive >> MinorVersion;
  240.       archive >> LANRoot;
  241.       archive >> NumberOfLoggedOnUsers;
  242.    }
  243. }
  244.  
  245. /*
  246. ** CWorkstationTransport stuff
  247. */
  248.  
  249. IMPLEMENT_SERIAL( CWorkstationTransport, CObject, 1 )
  250.  
  251. CWorkstationTransport::CWorkstationTransport()
  252. {
  253.    m_Initialize();
  254. }
  255.  
  256. CWorkstationTransport::~CWorkstationTransport()
  257. {
  258.    m_Initialize();
  259. }
  260.  
  261. void CWorkstationTransport::Copy( WKSTA_TRANSPORT_INFO_0 *source )
  262. {
  263.    ASSERT( source != NULL );
  264.  
  265.    if ( source == NULL )
  266.    {
  267.       m_Initialize();
  268.       return;
  269.    }
  270.  
  271. #if ! defined( UNICODE )
  272.    ::UNICODE_to_ASCII( (LPCWSTR) source->wkti0_transport_name,    source->wkti0_transport_name    );
  273.    ::UNICODE_to_ASCII( (LPCWSTR) source->wkti0_transport_address, source->wkti0_transport_address );
  274. #endif
  275.  
  276.    QualityOfService        = source->wkti0_quality_of_service;
  277.    NumberOfVirtualCircuits = source->wkti0_number_of_vcs;
  278.    Name                    = source->wkti0_transport_name;
  279.    Address                 = source->wkti0_transport_address;
  280.    WANish                  = source->wkti0_wan_ish;
  281.  
  282. #if ! defined( UNICODE )
  283.    ::ASCII_to_UNICODE( source->wkti0_transport_name,    (LPWSTR) source->wkti0_transport_name    );
  284.    ::ASCII_to_UNICODE( source->wkti0_transport_address, (LPWSTR) source->wkti0_transport_address );
  285. #endif
  286. }
  287.  
  288. void CWorkstationTransport::Empty( void )
  289. {
  290.    m_Initialize();
  291. }
  292.  
  293. void CWorkstationTransport::m_Initialize( void )
  294. {
  295.    QualityOfService        = 0;
  296.    NumberOfVirtualCircuits = 0;
  297.    Name.Empty();
  298.    Address.Empty();
  299.    WANish = FALSE;
  300. }
  301.  
  302. void CWorkstationTransport::Serialize( CArchive& archive )
  303. {
  304.    CObject::Serialize( archive );
  305.  
  306.    DWORD temp_value = 0;
  307.  
  308.    if ( archive.IsStoring() )
  309.    {
  310.       archive << QualityOfService;
  311.       archive << NumberOfVirtualCircuits;
  312.       archive << Name;
  313.       archive << Address;
  314.  
  315.       temp_value = WANish;
  316.       archive << temp_value;
  317.    }
  318.    else
  319.    {
  320.       archive >> QualityOfService;
  321.       archive >> NumberOfVirtualCircuits;
  322.       archive >> Name;
  323.       archive >> Address;
  324.       archive >> temp_value;
  325.       WANish = (BOOL) temp_value;
  326.    }
  327. }
  328.  
  329. /*
  330. ** CNetWorkstation Stuff
  331. */
  332.  
  333. IMPLEMENT_SERIAL( CNetWorkstation, CNetwork, 1 )
  334.  
  335. CNetWorkstation::CNetWorkstation()
  336. {
  337.    m_Initialize();
  338. }
  339.  
  340. CNetWorkstation::CNetWorkstation( LPCTSTR machine_name )
  341. {
  342.    m_Initialize();
  343.    Open( machine_name );
  344. }
  345.  
  346. CNetWorkstation::~CNetWorkstation()
  347. {
  348.    Close();
  349. }
  350.  
  351. void CNetWorkstation::Close( void )
  352. {
  353.    CNetwork::Close();
  354.  
  355.    if ( m_InformationBuffer100 != NULL )
  356.    {
  357.       ::NetApiBufferFree( m_InformationBuffer100 );
  358.       m_InformationBuffer100 = NULL;
  359.    }
  360.  
  361.    if ( m_InformationBuffer101 != NULL )
  362.    {
  363.       ::NetApiBufferFree( m_InformationBuffer101 );
  364.       m_InformationBuffer101 = NULL;
  365.    }
  366.  
  367.    if ( m_InformationBuffer102 != NULL )
  368.    {
  369.       ::NetApiBufferFree( m_InformationBuffer102 );
  370.       m_InformationBuffer102 = NULL;
  371.    }
  372.  
  373.    if ( m_TransportBuffer != NULL )
  374.    {
  375.       ::NetApiBufferFree( m_TransportBuffer );
  376.       m_TransportBuffer = NULL;
  377.    }
  378.  
  379.    if ( m_UserBuffer != NULL )
  380.    {
  381.       ::NetApiBufferFree( m_UserBuffer );
  382.       m_UserBuffer = NULL;
  383.    }
  384.  
  385.    m_TransportBuffer      = NULL;
  386.    m_UserBuffer           = NULL;
  387. }
  388.  
  389. void CNetWorkstation::m_Initialize( void )
  390. {
  391.    m_ErrorCode                     = 0;
  392.    m_InformationBuffer100          = NULL;
  393.    m_InformationBuffer101          = NULL;
  394.    m_InformationBuffer102          = NULL;
  395.    m_TransportBuffer               = NULL;
  396.    m_TransportResumeHandle         = 0;
  397.    m_TransportCurrentEntryNumber   = 0;
  398.    m_TransportNumberOfEntriesRead  = 0;
  399.    m_TransportTotalNumberOfEntries = 0;
  400.    m_UserBuffer                    = NULL;
  401.    m_UserResumeHandle              = NULL;
  402.    m_UserCurrentEntryNumber        = NULL;
  403.    m_UserNumberOfEntriesRead       = NULL;
  404.    m_UserTotalNumberOfEntries      = NULL;
  405. }
  406.  
  407. BOOL CNetWorkstation::EnumerateInformation( void )
  408. {
  409.    if ( m_InformationBuffer100 != NULL )
  410.    {
  411.       ::NetApiBufferFree( m_InformationBuffer100 );
  412.       m_InformationBuffer100 = NULL;
  413.    }
  414.  
  415.    if ( m_InformationBuffer101 != NULL )
  416.    {
  417.       ::NetApiBufferFree( m_InformationBuffer101 );
  418.       m_InformationBuffer101 = NULL;
  419.    }
  420.  
  421.    if ( m_InformationBuffer102 != NULL )
  422.    {
  423.       ::NetApiBufferFree( m_InformationBuffer102 );
  424.       m_InformationBuffer102 = NULL;
  425.    }
  426.  
  427.    m_ErrorCode = ::NetWkstaGetInfo( (LPTSTR) m_WideMachineName, 102, (LPBYTE *) &m_InformationBuffer102 );
  428.  
  429.    if ( m_ErrorCode == ERROR_ACCESS_DENIED )
  430.    {
  431.       m_ErrorCode = ::NetWkstaGetInfo( (LPTSTR) m_WideMachineName, 101, (LPBYTE *) &m_InformationBuffer101 );
  432.  
  433.       if ( m_ErrorCode == ERROR_ACCESS_DENIED )
  434.       {
  435.          m_ErrorCode = ::NetWkstaGetInfo( (LPTSTR) m_WideMachineName, 100, (LPBYTE *) &m_InformationBuffer100 );
  436.  
  437.          if ( m_ErrorCode != NERR_Success || m_InformationBuffer100 == NULL )
  438.          {
  439.             return( FALSE );
  440.          }
  441.       }
  442.       else if ( m_ErrorCode != NERR_Success || m_InformationBuffer101 == NULL )
  443.       {
  444.          return( FALSE );
  445.       }
  446.    }
  447.    else if ( m_ErrorCode != NERR_Success || m_InformationBuffer102 == NULL )
  448.    {
  449.       return( FALSE );
  450.    }
  451.  
  452.    return( TRUE );
  453. }
  454.  
  455. BOOL CNetWorkstation::EnumerateTransports( void )
  456. {
  457.    ::NetApiBufferFree( m_TransportBuffer );
  458.    m_TransportBuffer = NULL;
  459.  
  460.    m_TransportCurrentEntryNumber   = 0;
  461.    m_TransportNumberOfEntriesRead  = 0;
  462.    m_TransportResumeHandle         = 0;
  463.    m_TransportTotalNumberOfEntries = 0;
  464.  
  465.    m_ErrorCode = ::NetWkstaTransportEnum( (LPTSTR) m_WideMachineName, 
  466.                                                    0, 
  467.                                        (LPBYTE *) &m_TransportBuffer,
  468.                                                    65536,
  469.                                                   &m_TransportNumberOfEntriesRead,
  470.                                                   &m_TransportTotalNumberOfEntries,
  471.                                                   &m_TransportResumeHandle );
  472.  
  473.    if ( m_ErrorCode != NERR_Success || m_TransportBuffer == NULL || m_TransportNumberOfEntriesRead == 0 )
  474.    {
  475.       return( FALSE );
  476.    }
  477.  
  478.    return( TRUE );
  479. }
  480.  
  481. BOOL CNetWorkstation::EnumerateUsers( void )
  482. {
  483.    ::NetApiBufferFree( m_UserBuffer );
  484.    m_UserBuffer = NULL;
  485.  
  486.    m_UserCurrentEntryNumber   = 0;
  487.    m_UserNumberOfEntriesRead  = 0;
  488.    m_UserTotalNumberOfEntries = 0;
  489.    m_UserResumeHandle         = 0;
  490.  
  491.    m_ErrorCode = ::NetWkstaUserEnum( (LPTSTR) m_WideMachineName, 
  492.                                      1, 
  493.                          (LPBYTE *) &m_UserBuffer,
  494.                                      65536,
  495.                                     &m_UserNumberOfEntriesRead,
  496.                                     &m_UserTotalNumberOfEntries,
  497.                                     &m_UserResumeHandle );
  498.  
  499.    if ( m_ErrorCode != NERR_Success || m_UserBuffer == NULL || m_UserNumberOfEntriesRead == 0 )
  500.    {
  501.       return( FALSE );
  502.    }
  503.  
  504.    return( TRUE );
  505. }
  506.  
  507. BOOL CNetWorkstation::GetCurrentUser( CWorkstationUser& information )
  508. {
  509.    WKSTA_USER_INFO_1 *buffer = NULL;
  510.  
  511.    m_ErrorCode = ::NetWkstaUserGetInfo( NULL, 1, (LPBYTE *) &buffer );
  512.  
  513.    if ( m_ErrorCode != NERR_Success || buffer == NULL )
  514.    {
  515.       information.Empty();
  516.       return( FALSE );
  517.    }
  518.  
  519.    information.Copy( buffer );
  520.  
  521.    ::NetApiBufferFree( buffer );
  522.  
  523.    return( TRUE );
  524. }
  525.  
  526. BOOL CNetWorkstation::GetNext( CWorkstationInformation& information )
  527. {
  528.    if ( m_InformationBuffer100 != NULL )
  529.    {
  530.       information.Copy( m_InformationBuffer100 );
  531.       ::NetApiBufferFree( m_InformationBuffer100 );
  532.       m_InformationBuffer100 = NULL;
  533.       return( TRUE );
  534.    }
  535.  
  536.    if ( m_InformationBuffer101 != NULL )
  537.    {
  538.       information.Copy( m_InformationBuffer101 );
  539.       ::NetApiBufferFree( m_InformationBuffer101 );
  540.       m_InformationBuffer101 = NULL;
  541.       return( TRUE );
  542.    }
  543.  
  544.    if ( m_InformationBuffer102 != NULL )
  545.    {
  546.       information.Copy( m_InformationBuffer102 );
  547.       ::NetApiBufferFree( m_InformationBuffer102 );
  548.       m_InformationBuffer102 = NULL;
  549.       return( TRUE );
  550.    }
  551.  
  552.    information.Empty();
  553.    return( FALSE );
  554. }
  555.  
  556. BOOL CNetWorkstation::GetNext( CWorkstationTransport& information )
  557. {
  558.    if ( m_TransportCurrentEntryNumber < m_TransportTotalNumberOfEntries )
  559.    {
  560.       information.Copy( &m_TransportBuffer[ m_TransportCurrentEntryNumber ] );
  561.       m_TransportCurrentEntryNumber++;
  562.       return( TRUE );
  563.    }
  564.  
  565.    information.Empty();
  566.    return( FALSE );
  567. }
  568.  
  569. BOOL CNetWorkstation::GetNext( CWorkstationUser& information )
  570. {
  571.    if ( m_UserCurrentEntryNumber < m_UserTotalNumberOfEntries )
  572.    {
  573.       information.Copy( &m_UserBuffer[ m_UserCurrentEntryNumber ] );
  574.       m_UserCurrentEntryNumber++;
  575.       return( TRUE );
  576.    }
  577.  
  578.    information.Empty();
  579.    return( FALSE );
  580. }
  581.  
  582. void CNetWorkstation::Serialize( CArchive& archive )
  583. {
  584.    CNetwork::Serialize( archive );
  585.  
  586.    if ( archive.IsStoring() )
  587.    {
  588.       archive << m_ErrorCode;
  589.    }
  590.    else
  591.    {
  592.       archive >> m_ErrorCode;
  593.    }
  594. }
  595.